// ----------------------------------------------------------------------------
// serialize_schema.h
// ----------------------------------------------------------------------------
/*
 Description:
 データ構造を定義する為のクラス. 
 
 */
#pragma once
#include <fw/base/runtime/hash.h>
#include <fw/base/serialize/serialize_allocator.h>
#include <fw/base/serialize/serialize_status.h>
#include <fw/base/serialize/serialize_dictionary.h>
#include <fw/base/serialize/serialize_type.h>

class SerializeScalarValue;
class SerializeCompoundType;
class ByteReader;
class ByteWriter;

namespace fw
{
	
	class SerializeSchema
	{
	private:
		enum State
		{
			kState_New,
			kState_Building,
			kState_Ready,
		};
		
		State								m_state;
		SerializeCompoundType*				m_currentCompound;
		Hash								m_currentName;
		SerializeDictionary<SerializeType*>	m_dictionary;
		
	public:
		 SerializeSchema();
		~SerializeSchema();
		
		const SerializeType*	FindType(Hash name) const;
		SerializeType*			FindType(Hash name);
		
		void	Begin();
		void	End();
		
		void	BeginCompound(Hash typeName);
		void	EndCompound  ();
		
		char*	Convert(const char* srcData, const SerializeSchema& srcSchema, Hash name, SerializeAllocator& alloc) const;
		
		void	AddInstance			(Hash memberName, int count, Hash typeName);
		void	AddPointer			(Hash memberName, int count, Hash typeName);
		void	AddCounterPointer	(Hash memberName, int count, Hash typeName, Hash countName);
		void	AddStringPointer	(Hash memberName, int count, Hash typeName, Hash terminatorName, const SerializeScalarValue& terminatorValue);
		
		SerializeStatus	FixUp(Hash typeName);
		
		void					Write  (ByteWriter& writer) const;
		static SerializeSchema*	ReadNew(ByteReader& reader);
		
		uint64_t				GetChecksum(uint64_t basis=0) const;
		
	private:
		
		SerializeStatus			FixUp();
		void					AddType(Hash typeName, SerializeType* serializeType);
		
	}; // class Schema
	
} // namespace fw

